package br.ufrj.dcc.xml.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.io.File;
import java.util.HashMap;

import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;

public class XmlFileChooser extends JPanel {

	public static final int SAVE = 2;

	public static final int OPEN = 1;

	private static final long serialVersionUID = 1L;

	private String path;

	class AnOvalIcon implements Icon {
		Color color;

		public AnOvalIcon(Color c) {
			color = c;
		}

		public void paintIcon(Component c, Graphics g, int x, int y) {
			g.setColor(color);
			g.fillOval(x, y, getIconWidth(), getIconHeight());
		}

		public int getIconWidth() {
			return 10;
		}

		public int getIconHeight() {
			return 15;
		}
	}

	public class IconView extends FileView {
		private HashMap<String, AnOvalIcon> hash = new HashMap<String, AnOvalIcon>();

		public IconView() {
			hash.put("xml", new AnOvalIcon(Color.GREEN));
		}

		public String getName(File f) {
			String s = f.getName();
			if (s.length() == 0) {
				s = f.getAbsolutePath();
			}
			return s;
		}

		public String getDescription(File f) {
			return f.getName();
		}

		public String getTypeDescription(File f) {
			return f.getAbsolutePath();
		}

		public Icon getIcon(File f) {
			String path = f.getAbsolutePath();
			int pos = path.lastIndexOf('.');
			if ((pos >= 0) && (pos < (path.length() - OPEN))) {
				String ext = path.substring(pos + OPEN).toLowerCase();
				return (Icon) hash.get(ext);
			}
			return null;
		}

		public Boolean isTraversable(File file) {
			return (new Boolean(file.isDirectory()));
		}
	}

	public class ExtensionFilter extends FileFilter {
		private String extensions[];

		private String description;

		public ExtensionFilter(String description, String extension) {
			this(description, new String[] { extension });
		}

		public ExtensionFilter(String description, String extensions[]) {
			this.description = description;
			this.extensions = (String[]) extensions.clone();
		}

		public boolean accept(File file) {
			if (file.isDirectory()) {
				return true;
			}
			int count = extensions.length;
			String path = file.getAbsolutePath();
			for (int i = 0; i < count; i++) {
				String ext = extensions[i];
				if (path.endsWith(ext)
						&& (path.charAt(path.length() - ext.length()) == '.')) {
					return true;
				}
			}
			return false;
		}

		public String getDescription() {
			return (description == null ? extensions[0] : description);
		}
	}

	public XmlFileChooser(JTextArea pane, int tipo) {
		JFileChooser chooser = new JFileChooser(".");

		FileFilter xmlType = new ExtensionFilter("Arquivo XML", ".xml");

		chooser.addChoosableFileFilter(xmlType);

		FileView view = new IconView();
		chooser.setFileView(view);

		int status;
		switch (tipo) {
		case XmlFileChooser.OPEN:
			status = chooser.showOpenDialog(XmlFileChooser.this); 
			break;
		case XmlFileChooser.SAVE:
			status = chooser.showSaveDialog(XmlFileChooser.this);
			break;
		default:
			throw new IllegalArgumentException("Tipo inválido");
		}
		
		
		if (status == JFileChooser.APPROVE_OPTION) {
			path = chooser.getSelectedFile().getAbsolutePath();
		}
	}

	public String getPath() {
		return path;
	}
}